﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using LinkCard.Model;

namespace LinkCard.AdminCP.CRM
{
    public partial class Members : System.Web.UI.Page
    {
        public string sEmail = "";

        private int _totalRecords;

        private int nPageSize = 20;
        
        public string sFromDate = "";
        public string sToDate = "";
        
        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("/login.aspx");
            }

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["Email"] != null) { sEmail = Request.QueryString["Email"].Trim(); }

                DataBindPaging();
            }
        }
        private void DataBindPaging()
        {
            var db = new TopLinkEntities();
            
            int curpage = 0;
            int skip = 0;

            if (Request.QueryString["page"] != null)
            {
                if (Request.QueryString["page"].ToString() != "1")
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                    skip = curpage * nPageSize;
                }
                else
                {
                    curpage = 1;
                    skip = 0;
                }
            }
            else
            {
                curpage = 1;
                skip = 0;
            }

            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "dd/MM/yyyy";

            if (sFromDate != "")
            {
                sFromDate += " 00:00:00";
                dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

            }
            if (sToDate != "")
            {
                sToDate += " 23:59:59";
                dToDate = Convert.ToDateTime(sToDate, dateInfo);
            }

            //var _emailList = (from member in db.Members
            //               //where(t=> t.CustomerID == payment.CustomerID && payment.SupplierID == nSupplierID).ToList();
            //               where (member.DateCreated >= dFromDate
            //                     && member.DateCreated <= dToDate
            //                     && member.Email.Contains(sEmail))
            //               select new
            //               {
            //                   member.Email,
            //                   member.DateCreated,
            //                   member.Status
            //               }).OrderBy(t => t.DateCreated).Skip(skip).Take(nPageSize).ToList();

            //var paylog = _customer.ToList();

            var _emailList = db.Members.Where(t => t.DateCreated >= dFromDate && t.DateCreated <= dToDate && t.Email.Contains(sEmail)).OrderByDescending(t => t.DateCreated).Skip(skip).Take(nPageSize).ToList();

            if (_emailList.Count > 0)
            {
                _totalRecords = _emailList.Count;

                int nPageCount;

                if (_totalRecords % nPageSize == 0)
                {
                    nPageCount = _totalRecords / nPageSize;
                }
                else
                {
                    nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                }

                if (nPageCount < 2) divPager.Visible = false;

                PagerShow.Text = LinkCard.Utils.hash1way.ShowPageNavigation(curpage, nPageCount);

                msgList.DataSource = _emailList;

                msgList.DataBind();
                msgNoData.Visible = false;
            }
            else
            {

                msgList.DataSource = null;
                msgList.DataSourceID = null;
                msgList.DataBind();
                msgNoData.Visible = true;
            }
            
        }
        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemIndex < 0) return;
            //DataRowView row = (DataRowView)e.Item.DataItem;

            //var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            //chkDelete.Value = row["MemberID"].ToString();

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int nAction = int.Parse(ActionOption.Value);
            if (nAction == 1)
            {
                string sArray = "(0";

                foreach (RepeaterItem item in msgList.Items)
                {
                    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                    if (chkDelete.Checked)
                    {
                        sArray = sArray + "," + chkDelete.Value + "";
                    }
                }
                sArray = sArray + ")";

                string sSQL = "delete Members where MemberID in" + sArray;
                DataHelper.ExecuteNonQuery(sSQL, true);
                Response.Redirect("/CRM/Members.aspx");
            }
            if (nAction == 2)
            {
                string sArray = "(0";

                foreach (RepeaterItem item in msgList.Items)
                {
                    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                    if (chkDelete.Checked)
                    {
                        sArray = sArray + "," + chkDelete.Value + "";
                    }
                }
                sArray = sArray + ")";

                string sSQL = "Update Members set Status = 0 where MemberID in" + sArray;
                DataHelper.ExecuteNonQuery(sSQL, true);
                Response.Redirect("/CRM/Members.aspx");
            }
            if (nAction == 3)
            {
                string sArray = "(0";

                foreach (RepeaterItem item in msgList.Items)
                {
                    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                    if (chkDelete.Checked)
                    {
                        sArray = sArray + "," + chkDelete.Value + "";
                    }
                }
                sArray = sArray + ")";

                string sSQL = "Update Members set Status = 1 where MemberID in" + sArray;
                DataHelper.ExecuteNonQuery(sSQL, true);
                Response.Redirect("/CRM/Members.aspx");
            }
            if (nAction == 4)
            {
                Response.Redirect("export_member_excel.aspx?FromDate=" + sFromDate + "&ToDate=" + sToDate);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sEmail = this.Email.Value.Trim();
            if (sEmail == "Email") sEmail = "";
            sFromDate = this.FromDate.Value;
            sToDate = this.ToDate.Value;
            DataBindPaging();

        }
    }
}